home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 60.zip / BS1 part 60 / C mon v3 d3.adf / CMANV3_3.LHA / ACE9.lha / System / DirtyInput / Joystick.c < prev    next >
C/C++ Source or Header  |  1992-05-03  |  4KB  |  127 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Intuition               Amiga C Club       */
  7. /* Chapter: DirtyInput                  Tulevagen 22       */
  8. /* File:    Joystick.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-02                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* Joystick() is a handy, easy and fast but naughty function that hits */
  21. /* the hardware of the Amiga. It looks at either port 1 or port 2, and */
  22. /* returns a bitfield containing the position of the stick and the     */
  23. /* present state of the button.                                        */
  24. /*                                                                     */
  25. /* Synopsis: value = Joystick( port );                                 */
  26. /*                                                                     */
  27. /* value:    (UBYTE) If the fire button is pressed, the first bit is   */
  28. /*           set. If the stick is moved to the right, the second bit   */
  29. /*           is set, and if the stick is moved to the left, the third  */
  30. /*           bit is set. The fourth bit is set if the stick is moved   */
  31. /*           down, and the fifth bit is set if the stick is moved up.  */
  32. /*                                                                     */
  33. /* port:     (UBYTE) Set the flag PORT1 if you want to check the first */
  34. /*           (mouse) port, or set the flag PORT2 if you want to check  */
  35. /*           the second (joystick) port.                               */
  36.  
  37.  
  38.  
  39. #include <exec/types.h>
  40. #include <hardware/custom.h>
  41. #include <hardware/cia.h>
  42.  
  43.  
  44. #define FIRE   1
  45. #define RIGHT  2
  46. #define LEFT   4
  47. #define DOWN   8
  48. #define UP    16
  49.  
  50. #define PORT1 1
  51. #define PORT2 2
  52.  
  53.  
  54.  
  55. /* This will automatically be linked to the Custom structure: */
  56. extern struct Custom far custom;
  57.  
  58. /* This will automatically be linked to the CIA A (8520) chip: */
  59. extern struct CIA far ciaa;
  60.  
  61.  
  62.  
  63. void main();
  64. UBYTE Joystick( UBYTE port );
  65.  
  66.  
  67. void main()
  68. {
  69.   int timer = 0;
  70.   UBYTE value = 0;
  71.   UBYTE old_value = 0;
  72.   
  73.   
  74.   while( timer < 30 )
  75.   {
  76.     old_value = value;
  77.  
  78.     value = Joystick( PORT2 );
  79.     
  80.     if( value != old_value )
  81.     {
  82.       timer++;
  83.  
  84.       if( value & FIRE )
  85.         printf("FIRE ");
  86.       if( value & RIGHT )
  87.         printf("RIGHT ");
  88.       if( value & LEFT )
  89.         printf("LEFT ");
  90.       if( value & DOWN )
  91.         printf("DOWN ");
  92.       if( value & UP )
  93.         printf("UP ");
  94.       printf("\n");
  95.     }
  96.   }
  97. }
  98.  
  99.  
  100.  
  101. UBYTE Joystick( UBYTE port )
  102. {
  103.   UBYTE data = 0;
  104.   UWORD joy;
  105.   
  106.   if( port == PORT1 )
  107.   {
  108.     /* PORT 1 ("MOUSE PORT") */
  109.     joy = custom.joy0dat;
  110.     data += !( ciaa.ciapra & 0x0040 ) ? FIRE : 0;
  111.   }
  112.   else
  113.   {
  114.     /* PORT 2 ("JOYSTICK PORT") */
  115.     joy = custom.joy1dat;
  116.     data += !( ciaa.ciapra & 0x0080 ) ? FIRE : 0;
  117.   }
  118.  
  119.   data += joy & 0x0002 ? RIGHT : 0;
  120.   data += joy & 0x0200 ? LEFT : 0;
  121.   data += (joy >> 1 ^ joy) & 0x0001 ? DOWN : 0;
  122.   data += (joy >> 1 ^ joy) & 0x0100 ? UP : 0;
  123.  
  124.   return( data );
  125. }
  126.  
  127.